#coding;utf-8
import talib
from py_at.strategy import Strategy
from py_at.Data import Data
from py_at.Bar import Bar
import numpy as np
import time
class Breaker(Strategy):
    """
    策略说明:
        通过计算
    """
    def __init__(self,cfg):
        super().__init__(cfg)
        #初始化参数
        if cfg == "":
            self.Params['Lots'] =1
            self.Params['K1']=0.35
            self.Params['K2']=0.35
            self.Params['Slippage'] = 1
        self.lots=self.Params['Lots']
        self.bconn=0
        self.sconn=0
        self.UpperBand = 0
        self.LowerBand = 0
        self.timeD =""
        self.Varlist['upline']=[]
        self.Varlist['lowline'] = []


    def OnBarUpdate(self, data=Data, bar=Bar):
        #if bar.TD!=self.DateD[1]:
        if len(self.HighD)<2:
            return

        HH = self.HighD[1]
        LL = self.LowD[1]
        self.UpperBand = self.OpenD[0] + (HH - LL) * self.Params['K1']
        self.LowerBand = self.OpenD[0] - (HH - LL) * self.Params['K2']

        if self.D[-1]!=self.timeD:
            if len(self.Varlist['upline'])==0:    #填充策略初始化部分的数据  都填写为None
                for i in range(len(self.Bars)):
                    self.Varlist['upline'].append(None)
                    self.Varlist['lowline'].append(None)
            if self.Bars[-1].TD !=self.Bars[-2].TD:
                self.bconn=0
                self.sconn=0
            self.Varlist['upline'].append(self.UpperBand)
            self.Varlist['lowline'].append(self.LowerBand)
            self.IndexDict['UpperBand'] = self.Varlist['upline']
            self.IndexDict['LowerBand'] = self.Varlist['lowline']
            self.timeD=self.D[-1]


        # if(self.PositionLong==0 and self.H[-1]>=self.UpperBand and self.bconn<1 and self.Bars[-1].TD==self.Bars[-2].TD):   #self.Bars[-1].TD==self.Bars[-2].TD 为了控制在9:00 这个位置不开仓 等于去除小节
        #     self.Buy(max(self.UpperBand,self.O[-1]));
        #     self.bconn+=1
        #     return
        #
        # if(self.PositionLong==1 and self.L[-1]<=self.LowerBand):
        #     self.Sell(min(self.LowerBand,self.O[-1]));
        #     return
        #
        # if(self.PositionShort==0 and self.L[-1]<=self.LowerBand and self.sconn<1 and self.Bars[-1].TD==self.Bars[-2].TD):
        #     self.SellShort(min(self.LowerBand,self.O[-1]))
        #     self.sconn+=1
        #     return
        #
        # if(self.PositionShort==1 and self.H[-1]>=self.UpperBand):
        #     self.BuyToCover(max(self.UpperBand,self.O[-1]))
        #     return
        if (self.PositionShort >0 and self.H[-1] >= self.UpperBand):
            self.BuyToCover(max(self.UpperBand, self.O[-1]+self.Params['Slippage']), self.lots, '买平')

        if (self.PositionLong >0 and self.L[-1] <= self.LowerBand):
            self.Sell(min(self.LowerBand, self.O[-1]-self.Params['Slippage']), self.lots, '卖平')

        if(self.PositionLong==0 and self.H[-1]>=self.UpperBand and self.bconn<1 and self.Bars[-1].TD==self.Bars[-2].TD):   #self.Bars[-1].TD==self.Bars[-2].TD 为了控制在9:00 这个位置不开仓 等于去除小节
            self.Buy(max(self.UpperBand,self.O[-1]+self.Params['Slippage']),self.lots,'买开')
            self.bconn+=1

        if(self.PositionShort==0 and self.L[-1]<=self.LowerBand and self.sconn<1 and self.Bars[-1].TD==self.Bars[-2].TD):
            self.SellShort(min(self.LowerBand,self.O[-1]-self.Params['Slippage']),self.lots,'卖开')
            self.sconn+=1




